﻿using System;
using static System.Console;
using System.Threading;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Security.Cryptography;

namespace APP
{
    class Program
    {
        static int step = 30;
    public static long UnixTime(DateTime time)
        {
          long timeSinceStartTime=(long)(time.ToUniversalTime() -
                   DateTime.UnixEpoch).TotalSeconds;
            if (timeSinceStartTime >= 0)
            {
                return timeSinceStartTime / step;
            }
            else
            {
                return (timeSinceStartTime - (step- 1)) / step;
            }
        }
        static byte[] Key(string k)
        {
            return Base32.Decode(k);
        }
        static void Main(string[] args)
        {
            WriteLine(get(result("youareok")));

        }
        static long HashToInt(byte[] arr,int offset,int l=4)
        {
            string temp = "";
            for (int i = offset; i < offset + l;i++)
            {
                temp += arr[i].ToString("X2");
            }
            return Convert.ToInt64(temp, 16);
        }

        static string get(byte[] hash)
        {
            int offset = hash[hash.Length - 1] & 0xF;
            // Grab a positive integer value starting at the given offset.

            long truncatedHash = HashToInt(hash,offset) & 0x7FFFFFFF;
            long pinValue = truncatedHash % (int)Math.Pow(10,6);
            return pinValue.ToString().PadLeft(6,'0');
        }

        static  byte[] result(string key)
        {
            long state = UnixTime(DateTime.Now);
            byte[] arr = BitConverter.GetBytes(state);
            Array.Reverse(arr);
            byte[] k = Key(key);
            return new HMACSHA1(k).ComputeHash(arr);
        }
    }
}
